﻿Imports System.IO

Public Class Form6

    Dim PictureBoxSize As Size = New Size(530, 460)
    Dim TextFileName As String = "Form6Log.txt"

    Private Sub btnLoadPicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadPicture.Click

        ' 判斷開檔視窗最後是否按下 [確定] 鍵
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

            ' 載入圖檔至 PictureBox
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)

            ' 表單標題文字設為目前開啟的檔案路徑資訊
            Me.Text = "Picture Viewer (" & OpenFileDialog1.FileName & ")"

            Dim s As String

            ' 產生目前系統的日期與時間字串
            s = DateTime.Now.ToLongDateString() & vbTab &
                 DateTime.Now.ToLongTimeString() & vbNewLine &
                 OpenFileDialog1.FileName

            ' 將新讀取檔案的Log資訊加入 Log 文字方塊中
            Form6Log.txtLog.Text &= s

            ' 開啟文字檔並採取附加文字的方式寫入資料
            Using sw As StreamWriter = File.AppendText(TextFileName)
                sw.WriteLine(s) ' 寫入資料至緩衝區 並 自動換行
                sw.Flush()      ' 將緩衝區資料寫入檔案
                sw.Close()      ' 關閉檔案資源
            End Using           ' 自動釋放 Using 區塊內所使用的資源

        End If

    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        PictureBox1.SizeMode = PictureBoxSizeMode.Normal
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    End Sub

    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
        PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
    End Sub

    Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
        PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
    End Sub

    Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged
        PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
    End Sub

    Private Sub btnClearPicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearPicture.Click
        PictureBox1.Image = Nothing
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnResetSize_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResetSize.Click
        PictureBox1.Size = PictureBoxSize
    End Sub

    Private Sub Form6_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MessageBox.Show("確定要關閉嗎?", "訊息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            ' 告知系統取消關閉式窗
            e.Cancel = True
        End If
    End Sub

    Private Sub btnDisplayLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayLog.Click
        Form6Log.ShowDialog()
    End Sub

    Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' 檔案存在才處理
        If File.Exists(TextFileName) Then

            ' 開啟文字檔
            Using sr As StreamReader = File.OpenText(TextFileName)

                ' 一次讀取所有資料並放入 Log 文字方塊
                Form6Log.txtLog.Text = sr.ReadToEnd()

                ' 關閉檔案資源
                sr.Close()

            End Using ' 自動釋放 Using 區塊內所使用的資源

        End If
    End Sub

    Private Sub btnDeleteLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteLog.Click

        ' 檔案存在才處理
        If File.Exists(TextFileName) Then
            ' 詢問是否確定刪除
            If MessageBox.Show("確定要刪除 Log 嗎?", "訊息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
                ' 刪除檔案
                File.Delete(TextFileName)

                ' 清空文字方塊 Log 訊息
                Form6Log.txtLog.Text = ""
            End If
        End If

    End Sub

    Private Sub btnSaveAsLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAsLog.Click

        ' 設定 SaveFileDialog1 檔案路徑為目前程式的路徑
        SaveFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory()

        ' 判斷存檔視窗最後是否按下 [確定] 鍵
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

            ' 建立新文字檔
            Using sw As StreamWriter = File.CreateText(SaveFileDialog1.FileName)
                sw.Write(Form6Log.txtLog.Text) ' 寫入資料至緩衝區
                sw.Flush() ' 將緩衝區資料寫入檔案
                sw.Close() ' 關閉檔案資源
            End Using ' 自動釋放 Using 區塊內所使用的資源

        End If
    End Sub
End Class